<?php
namespace App\Repositories;

use App\Models\Admin\CategoryModel;
use App\Models\AreaModel;

class BaseRepository
{
    protected $repository;

    public function createTime()
    {
        return $this->repository->createTime();
    }

    /**
     * 通过cateId，获取cateName
     */
    public function getCateNameById($cateId)
    {
        $model = CategoryModel::where('id',$cateId)->first();
        return $model ? $model->name : '';
    }

    /**
     * 通过Pid，获取cateList
     */
    public function getCatesByPid($pid)
    {
        $models = CategoryModel::where('del',0)
            ->where('pid',$pid)->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $key => $model) {
            $dataArr[$key]['id'] = $model->id;
            $dataArr[$key]['name'] = $model->name;
        }
        return $dataArr;
    }

    /**
     * 获取城市省市区
     */
    public function getCitys($pid=100000,$level=1)
    {
        $models = AreaModel::where('pid',$pid)
            ->where('level',$level)
            ->orderBy('id','desc')
            ->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        $dataArr[0] = [
            'id' => 0,
            'name' => '选择',
        ];
        foreach ($models as $key => $model) {
            $dataArr[$key+1]['id'] = $model->id;
            $dataArr[$key+1]['name'] = $model->name;
        }
        return $dataArr;
    }

    /**
     * 获取城市父级
     */
    public function getCityParent($areaId)
    {
        if (!$areaId) { return ''; }
        $model = AreaModel::where('id',$areaId)->first();
        $model2 = AreaModel::where('id',$model->pid)->first();
        $model3 = AreaModel::where('id',$model2->pid)->first();
        return array(
            'provice' => array(
                'id' => $model3->id,
                'name' => $model3->name,
            ),
            'city' => array(
                'id' => $model2->id,
                'name' => $model2->name,
            ),
            'area' => array(
                'id' => $model->id,
                'name' => $model->name,
            ),
        );
    }

    /**
     * 通过字符串，获取数组
     */
    public function getCityListByStr($addressStr)
    {
        if (!$addressStr) { return ''; }
        $addresss = explode('，',$addressStr);
        $provices = array();
        $citys = array();
        $areas = array();
        if ($addresss[0]) {  //省级
            $model = AreaModel::where('name',$addresss[0])->first();
            $provices['id'] = $model ? $model->id : '0';
            $provices['name'] = $model ? $model->name : '未定义';
        } else {
            $provices['id'] = '0';
            $provices['name'] = '未定义';
        }
        if ($addresss[1]) {  //市级
            $model2 = AreaModel::where('name', $addresss[1])->first();
            $citys['id'] = $model2 ? $model2->id : '0';
            $citys['name'] = $model2 ? $model2->name : '未定义';
        } else {
            $citys['id'] = '0';
            $citys['name'] = '未定义';
        }
        if ($addresss[2]) {  //区级
            $model3 = AreaModel::where('name',$addresss[2])->first();
            $regions['id'] = $model3 ? $model3->id : '0';
            $regions['name'] = $model3 ? $model3->name : '未定义';
        } else {
            $regions['id'] = '0';
            $regions['name'] = '未定义';
        }
        return array(
            'provice' => $provices,
            'city' => $citys,
            'area' => $regions,
        );
//        return array(
//            'provice' => array(
//                'id' => $model->id,
//                'name' => $model->name,
//            ),
//            'city' => array(
//                'id' => $model2->id,
//                'name' => $model2->name,
//            ),
//            'area' => array(
//                'id' => $model3->id,
//                'name' => $model3->name,
//            ),
//        );
    }

    /**
     * 把数据的对象格式转换成数据格式
     * @param $object
     * @return mixed
     */
    public function objToArr($obj)
    {
        if (is_string($obj)) {
            $obj = json_decode($obj);
        }
        $arr = array();
        foreach($obj as $k=>$v) {
            if(is_object($v) || is_array($v)) {
                $arr[$k] = $this->objToArr($v);
            } else {
                $arr[$k] = $v ? $v : '';
            }
        }
        return $arr;
    }
}